package com.cs157a.dao;

import java.sql.Array;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * This DAO is used for accessing data objects from the RADCRX table.
 * 
 * @author Hiep
 * 
 */
public class RadcrxDao {
	public Radcrx getObject(Connection conn, int rlink)
			throws NotFoundException, SQLException {
		Radcrx valueObject = new Radcrx();

		valueObject.setRlink(rlink);

		load(conn, valueObject);

		return valueObject;
	}

	public void load(Connection conn, Radcrx valueObject)
			throws NotFoundException, SQLException {

		String sql = "SELECT * FROM RADCRX WHERE (RLINK = ? ) ";
		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);
			stmt.setInt(1, valueObject.getRlink());

			singleQuery(conn, stmt, valueObject);

		} finally {
			if (stmt != null)
				stmt.close();
		}
	}

	protected void singleQuery(Connection conn, PreparedStatement stmt,
			Radcrx valueObject) throws NotFoundException, SQLException {

		ResultSet result = null;

		try {
			result = stmt.executeQuery();

			if (result.next()) {

				valueObject.setArs1(result.getString("ARS1"));
				valueObject.setArs2(result.getString("ARS2"));
				valueObject.setArs3(result.getString("ARS3"));
				valueObject.setArs4(result.getString("ARS4"));

				valueObject.setBeh1(result.getString("BEH1"));
				valueObject.setBeh2(result.getString("BEH2"));
				valueObject.setBeh3(result.getString("BEH3"));
				valueObject.setBeh4(result.getString("BEH4"));

				valueObject.setDan1(result.getString("DAN1"));
				valueObject.setDan2(result.getString("DAN2"));
				valueObject.setDan3(result.getString("DAN3"));
				valueObject.setDan4(result.getString("DAN4"));

				valueObject.setMod1(result.getString("MOD1"));
				valueObject.setMod2(result.getString("MOD2"));
				valueObject.setMod3(result.getString("MOD3"));
				valueObject.setMod4(result.getString("MOD4"));

			} else {
				throw new NotFoundException("RADCRX Object Not Found!");
			}
		} finally {
			if (result != null)
				result.close();
			if (stmt != null)
				stmt.close();
		}
	}

	public List<String> getAllRlink(Connection connection) throws SQLException {
		String sql = "Select RLINK from RADCRX";
		List<String> list = new ArrayList<String>();
		PreparedStatement stmt = null;
		stmt = connection.prepareStatement(sql);
		ResultSet res = stmt.executeQuery();
		while (res.next()) {
			list.add(res.getString("RLINK"));
		}
		return list;

	}

	public void updatePartNumber(Connection connection, String newPn,
			String oldPn) throws SQLException {
		// TODO Auto-generated method stub
		String sql = "update RADCRX set MOD1=" + newPn + " where MOD1=" + oldPn;

		Statement statement = connection.createStatement();
		statement.executeQuery(sql);
		connection.commit();

		sql = "update RADCRX set MOD2=" + newPn + " where MOD2=" + oldPn;
		statement.executeQuery(sql);
		connection.commit();

		sql = "update RADCRX set MOD3=" + newPn + " where MOD3=" + oldPn;
		statement.executeQuery(sql);
		connection.commit();

		sql = "update RADCRX set MOD4=" + newPn + " where MOD4=" + oldPn;
		statement.executeQuery(sql);
		connection.commit();

		sql = "update RADCRX set ARS1=" + newPn + " where ARS1=" + oldPn;
		statement.executeQuery(sql);
		connection.commit();

		sql = "update RADCRX set ARS2=" + newPn + " where ARS2=" + oldPn;
		statement.executeQuery(sql);
		connection.commit();

		sql = "update RADCRX set ARS3=" + newPn + " where ARS3=" + oldPn;
		statement.executeQuery(sql);
		connection.commit();

		sql = "update RADCRX set ARS4=" + newPn + " where ARS4=" + oldPn;
		statement.executeQuery(sql);
		connection.commit();

	}

	public int getNextRlink(Connection connection) throws SQLException {
		// TODO Auto-generated method stub
		String sql = "Select MAX(RLINK) MAX from RADCRX";
		Statement statement = connection.createStatement();
		ResultSet res = statement.executeQuery(sql);

		int next = 10000;
		if (res.next()) {
			next = res.getInt("MAX") + 1;
		}

		return next;
	}

	public void create(Connection connection, Radcrx radcrx)
			throws SQLException {
		// TODO Auto-generated method stub
		String sql = "insert into RADCRX values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement statement = connection.prepareStatement(sql);
		statement.setInt(1, radcrx.getRlink());
		statement.setString(2, radcrx.getArs1());
		statement.setString(3, radcrx.getArs2());
		statement.setString(4, radcrx.getArs3());
		statement.setString(5, radcrx.getArs4());
		statement.setString(6, radcrx.getMod1());
		statement.setString(7, radcrx.getMod2());
		statement.setString(8, radcrx.getMod3());
		statement.setString(9, radcrx.getMod4());
		statement.setString(10, radcrx.getBeh1());
		statement.setString(11, radcrx.getBeh2());
		statement.setString(12, radcrx.getBeh3());
		statement.setString(13, radcrx.getBeh4());
		statement.setString(14, radcrx.getDan1());
		statement.setString(15, radcrx.getDan2());
		statement.setString(16, radcrx.getDan3());
		statement.setString(17, radcrx.getDan4());

		statement.addBatch();
		statement.executeBatch();
		connection.commit();

	}

}
